package com.detrum.detrumdx.base;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.detrum.base.BaseStatic;
import com.detrum.detrumdx.R;
import com.detrum.utils.CrashHandler;
import com.detrum.utils.LogUtils;

import java.lang.reflect.Method;

/**
 * Created by abc on 2018/1/29.
 */

public class DetrumApp extends Application {
    public SharedPreferences sp;
    private static DetrumApp instance;
    private WindowManager mWindowManager;

    public static boolean isFinishAll;
    public static boolean controlIsFinish;
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        //SDKInitializer.initialize(this);
        instance = this;
        DisplayMetrics metrics = new DisplayMetrics();
        mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        Display display = mWindowManager.getDefaultDisplay();
        display.getMetrics(metrics);
        BaseStatic.SCREEN_WIDTH = metrics.widthPixels;//屏幕宽度（像素）
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, metrics);
            BaseStatic.SCREEN_HEIGHT = metrics.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //BaseStatic.SCREEN_HEIGHT = metrics.heightPixels;//屏幕高度(像素)
        LogUtils.e(BaseStatic.SCREEN_WIDTH+"  * "+BaseStatic.SCREEN_HEIGHT);
        sp = getSharedPreferences(getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
        BaseStatic.appVersion = getVersionName(instance);
        BaseStatic.appCode = getVersionCode(instance);
        CrashHandler.init(this);

    }
    //获取App单例（唯一）
    public static synchronized DetrumApp appInstance() {
        return instance;
    }

    //版本名
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;
        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pi;
    }

    public int getHasVirtualKey() {
        int dpi = 0;
        Display display = mWindowManager.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        @SuppressWarnings("rawtypes")
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            dpi = dm.heightPixels;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dpi;
    }
}
